<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理课程</title>
    <script src="js/base.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        h1, h6 {
            text-align: center;
            color: #333;
        }
        h1 span {
            color: #007BFF;
            font-weight: bold;
        }
        table {
            width: 100%;
            max-width: 800px;
            margin: 20px auto;
            border-collapse: collapse;
            text-align: center;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
        }
        th {
            background-color: #007BFF;
            color: white;
        }
        td a {
            color: #FF5722;
            text-decoration: none;
            font-weight: bold;
        }
        td a:hover {
            text-decoration: underline;
        }
        #buttons {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }
        button {
            padding: 8px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<h1>管理课程号为<span id="courseId"></span>的课程选课情况</h1>
<h6>当前<span id="stuCount"></span>人，容量<span id="maxLimit"></span>人。</h6>
<table border="1px" id="students">
    <tr>
        <td>学号</td>
        <td>学生用户名</td>
        <td>操作</td>
    </tr>

</table>
<div id="buttons"></div>
<script>
    let courseId=localStorage.getItem("managerCourseId");
    let stuCount=localStorage.getItem("stuCount");
    let maxLimit=localStorage.getItem("maxLimit");
    document.getElementById("courseId").innerHTML=courseId;

    document.getElementById("stuCount").innerHTML=stuCount;

    document.getElementById("maxLimit").innerHTML=maxLimit;


    let pageNo=1;
    let pageSize=5;
    let pageCount=0;
    let arr=null;
    function loadData(){

        let url="http://localhost:8080/teacher/pageByCourseId?pageNo="+pageNo+"&pageSize="+pageSize+"&courseId="+courseId;
        $.rest(url,"get",null,function (r){
            if (r.code==200){
                arr=r.data.data;
                pageCount=r.data.pageCount;
                let studentsInnerHTML="<tr>\n" +
                    "        <td>学号</td>\n" +
                    "        <td>学生用户名</td>\n" +
                    "        <td>操作</td>\n" +
                    "    </tr>";
                for(let i=0;i<arr.length;i++){
                    let item=arr[i];
                    studentsInnerHTML=studentsInnerHTML+"<tr>" +
                        "<td>"+item.id+"</td>" +
                        "<td>"+item.name+"</td>" +
                        "<td><a href='#' onclick='return onDelClick("+(i)+")'>删除</a></td>" +
                        "</tr>";
                }
                document.getElementById("students").innerHTML= studentsInnerHTML;

                let buttonsInnerHTML="";
                if (pageCount>0){
                    buttonsInnerHTML="<button onclick='onPrevClick();'>上一页</button>";
                    for(let i=0;i < pageCount;i++){
                        buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onPageClick("+(i+1)+")'>"+(i+1)+"</button>";
                    }

                    buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onNextClick();'>下一页</button>";
                }
                document.getElementById("buttons").innerHTML=buttonsInnerHTML;

            }
        });
    }
    loadData();
    function onPageClick(pNo){
        pageNo=pNo;
        loadData();

    }
    function onPrevClick(){
        if (pageNo==1){
            return;
        }
        pageNo--;
        loadData();
    }
    function onNextClick(){
        if (pageNo==pageCount){
            return
        }
        pageNo++;
        loadData();
    }
    function onDelClick(index){
        let stuId=arr[index].id;
        let url="http://localhost:8080/teacher/delStudentWithCourse?stuId="+stuId+"&courseId="+courseId;
        $.rest(url,"get",null,function (r){
            if (r.code==200){
                alert("删除成功");
                document.getElementById("stuCount").innerHTML=parseInt(document.getElementById("stuCount").innerHTML)-1;
                loadData();
            }
        });
    }
</script>

</body>
</html>